function localMoveEvent(exec) {
    if($.jStorage.get('MOVE_EVENT') != null) {
        var calEvent = {
            'MOVE_EVENT':$.jStorage.get('MOVE_EVENT')
        }; 
        return ajaxSuccessFail(WEBROOT+"/reattribute_event", calEvent, function(data) {
            $.jStorage.deleteKey('MOVE_EVENT');
            return exec();
        }); 
    }
    return exec();
}

function localResizeEvent(exec) {
    if($.jStorage.get('RESIZE_SLOT') != null) {
        var calEvent = {
            'RESIZE_SLOT':$.jStorage.get('RESIZE_SLOT')
        }; 
        return ajaxSuccessFail(WEBROOT+"/reattribute_event", calEvent, function(data) {
            $.jStorage.deleteKey('RESIZE_SLOT');
            return exec();
        }); 
    }
    return exec();
}

function localSaveEvent(exec) { 
    if($.jStorage.get('SAVE_EVENT') != null) {
        var calEvent = {
            'SAVE_EVENT':$.jStorage.get('SAVE_EVENT')
        };
        return ajaxSuccessFail(WEBROOT+"/save_event", calEvent, function(data) {
            $.jStorage.deleteKey('SAVE_EVENT');
            return exec();
        }); 
    } 
    return exec();
} 

function localCreateClient(exec) {
    if($.jStorage.get('CREATE_CLIENT') != null) {
        var postCreateClient = {
            'CREATE_CLIENT':$.jStorage.get('CREATE_CLIENT')
        };
        return ajaxSuccessFail(WEBROOT+"/getdata/create_client", postCreateClient, function(data) {
            if(data.length) {
                arrayId = json_decode(data);
                var arrayUniqId = new Array();
                var arrayUniqIdNames = {};
                // Update client array
                /////////////////////
                for(var uniqid in arrayId) {
                    arrayUniqId.push(uniqid);
                    for(i=0; i<cli.length; i++) {
                        if(cli[i].value == uniqid) {
                            cli[i].value = arrayId[uniqid];
                            arrayUniqIdNames[uniqid] = cli[i].label;
                        }
                    }
                }
                // Update prestations array if possible
                ////////////////////////////////////////
                events = $.jStorage.get('SAVE_EVENT');
                if(events != null) {
                    // Update client id
                    for(i=0; i<events.length; i++) {
                        if(events[i].client_id != undefined && in_array(events[i].client_id, arrayUniqId)) {
                            events[i].nom_client = arrayUniqIdNames[events[i].client_id];
                            events[i].prenom_client = '';
                            events[i].client_id = arrayId[events[i].client_id];
                            replace_client_and_name_id(events[i].id, events[i].client_id, events[i].nom_client);
                        }
                    }
                }
                // Set back in stack edited events
                $.jStorage.set('SAVE_EVENT', events); 
            } 
            $.jStorage.deleteKey('CREATE_CLIENT');
            return exec();
        });
    }
    return exec();
}

function localDeleteEvent(exec) {
    if($.jStorage.get('DELETE_EVENT') != null) {
        var calEvent = {
            'DELETE_EVENT':$.jStorage.get('DELETE_EVENT')
        };
        return ajaxSuccessFail(WEBROOT+"/delete_event", calEvent, function(data) {
            $.jStorage.deleteKey('DELETE_EVENT');
            exec(data);
        });
    }
    return exec(); 
}

function localSaveNote(exec) {
    if($.jStorage.get('SAVE_NOTE') != null) {
        var saveNotes = {
            'SAVE_NOTE':$.jStorage.get('SAVE_NOTE')
        };
        return ajaxSuccessFail(WEBROOT+'/FastService/SaveNotes.php', saveNotes, function(data) {
            $.jStorage.deleteKey('SAVE_NOTE');
            exec(data);
        });
    }
    return exec(); 
}

function resynchronizeAgendas(exec) { 
    // slot is array
    var ajaxUrl = WEBROOT+'/FastService/EventList.php';
    var post_array = {};                                                                                                                                                                                                                                   
    post_array['start_time']  = window.calendarCache['startTime'];
    post_array['end_time']  = window.calendarCache['endTime'];
    post_array['agendas']  = window.authorizedAgendas; 
    post_array['last_sync']  = window.calendarCache['last_sync']; 
    return ajaxForce(ajaxUrl, post_array, function(data) {
        result = json_decode(data);
        if(result != null) {
            add_replace_events(result['presta']);
            add_replace_events(result['web']);
            add_replace_events(result['slot']);
            remove_events(result['delete']);
            // Refresh
            return exec();
        }
        exec();
        return 0;
    });
    
}



// Utils
//////////////////
function arrayReplace(data, find, replace) {
    for(var i=0; i<data.length; i++) {
        if(data[i]['value'].toString() == find.toString()) {
            data[i]['value'] = replace;
        }
    }  
    return null;
} 

function arrayFinder(data, find) {
    for(var i=0; i<data.length; i++) {
        if(data[i]['value'].toString() == find.toString()) {
            return data[i];
        }
    }  
    return null;
} 

function getUserIdFromAgendaId(agendaId) {
    for(var i=0; i<window.agendaIdArray.length;i++) {
        if(window.agendaIdArray[i] == agendaId) {
            return i;
        }
    }
    return null;
}
